<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js">&lt;?php
<span id='global-property-'>/**
</span> * Cube PHP Framework
 * 
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the &quot;License&quot;); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * @author Dillen / Steffen
 */

namespace Library;
 
<span id='Library-ArrayObject'>/**
</span> * Library.ArrayObject provides an interface, so that you can access objects as arrays.
 * 
 * # Example:
 * 	// A simple 2d array
 * 	$config = array(
 * 		'name'	=&gt; 'John',
 * 		'age'	=&gt; '30',
 * 		'cars'	=&gt; array (
 * 			'0' =&gt; 'BMW',
 * 			'1' =&gt; 'Porsche'
 * 		)
 * 	);
 * 
 * 	// Here, we convert the array to an ArrayObject and store it in the config property of a class
 * 	$this-&gt;config = new \Library\ArrayObject($config);
 *  
 * 	// Test it out :)
 * 	echo $this-&gt;config['name']; // Output: John
 * 	echo $this-&gt;config['cars']['0']; // Output: BMW
 * 	echo $this-&gt;config['non_existing_index']; // Output: null
 * 
 * You can see that this is very powerfull! You don't need to check if the index exists because it won't throw an error.
 * If an index doesn't exist, it will return null instead.
 * 
 * @class Library.ArrayObject
 * @mixin ArrayAccess
 */
class ArrayObject implements \ArrayAccess
{
    protected $_storage = array();

	public function __clone() 
	{
		foreach ($this-&gt;_storage as $key =&gt; $value)
		{
			if ($value instanceof self)
			{
				$this-&gt;_storage[$key] = clone $value;
			}
		}
	}

	public function __construct(array $_storage = array()) 
	{
		foreach ($_storage as $key =&gt; $value)
		{
			$this-&gt;_storage[$key] = $value;
		}
	}

	public function offsetSet($offset, $_storage) 
	{
		if (is_array($_storage))
		{
			$_storage = new self($_storage);
		}
		
		if ($offset === null) 
		{
			$this-&gt;_storage[] = $_storage;
		} 
		else 
		{
			$this-&gt;_storage[$offset] = $_storage;
		}
	}

	public function toArray() 
	{
		$_storage = $this -&gt; _storage;
		
		foreach ($_storage as $key =&gt; $value)
		{
			if ($value instanceof self)
			{
				$_storage[$key] = $value -&gt; toArray();
			}
		}
		
		return $_storage;
	}

	public function offsetGet($offset) 
	{
		if (isset($this-&gt;_storage[$offset]))
		{
			return $this-&gt;_storage[$offset];
		}
		
		if (!isset($this-&gt;_storage[$offset]))
		{
			$this-&gt;_storage[$offset] = new self;
		}
		
		return $this-&gt;_storage[$offset];
	}

	public function offsetExists($offset) 
	{
		return isset($this-&gt;_storage[$offset]);
	}

	public function offsetUnset($offset) 
	{
		unset($this-&gt;_storage);
	}
}</pre>
</body>
</html>
